<?php


namespace App\Http\Services\Admin\Account;


use App\Http\Constants\Account\CommonCoinCoinConstants;
use App\Http\Constants\Account\CommonCoinCoinLogConstants;
use App\Http\Services\Admin\CommonService;
use App\Models\CommonCoinCoin;
use App\Models\CommonCoinCoinLog;
use YiluTech\ComplexSearch\ComplexSearch;

class CommonCoinCoinService extends ComplexSearch
{
    public $root = CommonCoinCoin::class;
    protected $orderBy = 'id desc';
    protected $display = 'simple';

    protected $username = null;

    protected $headers = [
        ['label' => '红包账户ID', 'value' => 'id'],
        ['label' => '会员ID', 'value' => 'member_id'],
        ['label' => '会员昵称', 'value' => 'nickname'],
        ['label' => '会员手机号', 'value' => 'username'],
        ['label' => '状态', 'value' => 'status'],
        ['label' => '红包余额', 'value' => 'now_coin'],
        ['label' => '冻结金额', 'value' => 'freeze_coin'],
        ['label' => '创建时间', 'value' => 'create_time'],
        ['label' => '更新时间', 'value' => 'update_time'],
    ];
    protected $conditions = [
        'member_id' => [
            'label' => '会员ID',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'id' => [
            'label' => '红包账户ID',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'username' => [
            'label' => '会员手机号',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'status' => [
            'label' => '状态',
            'ctype' => 'select',
            'operator' => CommonCoinCoinConstants::ACCOUNT_TYPE,
            'custom' => true,
        ],
        'create_time' => [
            'label' => '创建时间',
            'ctype' => 'date-in',
            'format' => 'Y-m-d H:i',
            'display' => true,
        ],
    ];

    public function make($request)
    {
        if ($request['action'] !== 'prepare') {
            if (!empty($request['params'])) {
                foreach ($request['params'] as $k => $v){
                    if ($v[0] == 'username'){
                        $this->username = $v[2];
                        unset($request['params'][$k]);
                    }

                }
            }
        }


        return parent::make($request);
    }

    public function query()
    {
        $select = ['id', 'member_id', 'status', 'now_coin', 'freeze_coin', 'create_time'];
        $query = parent::query();
        $query->select($select);
        if (!is_null($this->username)) {
            $userInfo = \SharedCache::memberInfoByPhone($this->username);
            if (!empty($userInfo['id'])) {
                $query->where('member_id', $userInfo['id']);
            }
        }

        return $query;
    }

    public function get()
    {
        $result = parent::get();
        if ($this->action !== 'prepare') {
            $result = $result->toArray();
            if (!empty($result['data'])) {
                foreach ($result['data'] as $k => &$v) {
                    $member = \SharedCache::getMember($v['member_id']);
                    $v['username'] = $member['username'];
                    $v['nickname'] = $member['nickname'];
                }
            }
            $result = CommonService::objRegroupArr($result);
        }

        return $result;

    }


    public function detail($id)
    {
        $data = CommonCoinCoin::find($id);
        $member = \SharedCache::getMember($data['member_id']);
        $data['username'] = $member['username'];
        $data['nickname'] = $member['nickname'];
        $result['coinAccountDetail'] = $data;
        $result['coinAccountList'] = [];
        return $result;
    }


}
